define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'integral_order/index' + location.search,
                    add_url: 'integral_order/add',
                    edit_url: 'integral_order/edit',
                    del_url: '',
                    multi_url: 'integral_order/multi',
                    import_url: 'integral_order/import',
                    table: 'integral_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'goods.cover', title: __('Goods.cover'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        {field: 'region.name', title: __('Region.name'), operate: 'LIKE', searchable: true},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'number', title: __('Number'), operate: false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'deliverytime', title: __('Deliverytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'receivingtime', title: __('Receivingtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('Detail'),
                                    icon: '',
                                    classname: 'btn btn-xs btn-info btn-editone',
                                    url: 'integral_order/edit',
                                    refresh: true
                                },
                                {
                                    name: 'deliver',
                                    text: __('Deliver'),
                                    icon: '',
                                    classname: 'btn btn-xs btn-warning btn-ajax',
                                    url: 'integral_order/multi/params/status=1',
                                    refresh: true
                                },
                                {
                                    name: 'completed',
                                    text: __('Completed'),
                                    icon: '',
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    url: 'integral_order/multi/params/status=2',
                                    refresh: true
                                }
                            ],
                            formatter: function (value, row, index) {
                                // 默认按钮组
                                var buttons = $.extend([], this.buttons || []);
                                // 自定义按钮
                                var customs = [];
                                buttons.forEach(function (item) {
                                    switch (item.name) {
                                        case 'deliver':
                                            row.status === 0 && customs.push(item);
                                            break;
                                        case 'completed':
                                            row.status === 1 && customs.push(item);
                                            break;
                                        default:
                                            customs.push(item);
                                            break;
                                    }
                                });
                                return Table.api.buttonlink(this, customs, value, row, index, 'operate');
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});